<template>
	<view class="page p15">
		<view>
			<view v-for="(item,index) in list" :key="index" class="bg-white p20 brs5 mb15" @click="toSelect(item)">
				<view class="fz16 u-font-bold u-f-item u-f-jsb">
					车牌号：{{item.code}}
					<u-icon v-if="!type" name="trash" color="#999" size="36" @click="toDel(item)"></u-icon>
				</view>
			</view>
		</view>
		<view class="module-btn u-flex fixed-bottom" v-if="!type">
			<button class="u-f-justify u-reset-button btn" @click="toAdd">添加车辆</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				type:'',
				loadStatus: '',
				list: []
			}
		},
		computed: {},
		onLoad(e) {
			if(e.type){
				this.type=e.type
			}
		},
		onShow() {
			this.initData()
		},
		// 页面下拉到底部触发
		onReachBottom() {},
		methods: {
			toAdd(){
				uni.navigateTo({
					url:'/pages/user/car/add'
				})
			},
			toSelect(item){
				if(this.type){
					this.$db.set('carInfo',item)
					uni.navigateBack()
				}
			},
			toDel(item){
				
				this.$common.modelShow('提示', '确认删除该车辆吗?', () => {
					let data = {
						id: item.id
					}
				
					this.$api.carDel(data, res => {
						if (res.code) {
							this.$common.successToShow('操作成功', () => {
				
								this.initData()
							})
						} else {
							this.$common.errorToShow(res.msg)
						}
					})
				})
			},
			initData() {
				this.page = 1
				this.list = []
				this.getList()
			},
			getList() {


				this.$api.carList({
					page: this.page,
				}, res => {
					if (res.code) {

						let _list = res.data

						this.list = [...this.list, ..._list]

						console.log(this.list)
						// 判断所有数据是否请求完毕
						if (res.data.total > this.list.length) {
							this.page++
							this.loadStatus = 'more'
						} else {
							this.loadStatus = 'noMore'
						}
					} else {
						this.$common.errorToShow(res.msg)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.btn {
		background: #ffd100;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		color: #fff;
		width: 100%;
		margin: 0 40rpx;
		height: 90rpx;
	}
</style>